package com.mitgroup.dmrvs.webservice;

import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;
import org.kxml2.kdom.Element;
import org.kxml2.kdom.Node;
import org.xmlpull.v1.XmlPullParserException;

public class WSClient {
	/**
	 * webservice命名空间
	 */
	public static final String nameSpace = "http://services.webservice.mybatis.ds.com/";
	/**
	 * webservice地址
	 */
	 public static final String wsBaseUrl= "http://www.welion.net/dmrvsWS/service/IDMRVS";
	//public static final String wsBaseUrl= "http://192.168.206.83:7001/dmrvsWS/service/IDMRVS";
	/**
	 * 访问图片路径
	 */
	public static final String imageUrl= "http://www.welion.net/uploadPhoto/";
	//public static final String imageUrl= "http://192.168.206.83:7001/dmrvsWS/uploadPhoto/";
	/**
	 * webservice认证用户名
	 */
	public static final String WS_UNAME = "ars";
	/**
	 * webservice认证密码
	 */
	public static final String WS_PWD = "ars3g";
	public static String soapGetInfo(String wsUrl,String methodName,String[] key,String[] value )
	{
		String result="";
		try
		{
			SoapObject request = new SoapObject(nameSpace, methodName);
			//		request.addProperty("userName1", "李四");
			//传参
			for(int i = 0;i<key.length;i++){
				request.addProperty(key[i], value[i]);
			}
			SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
			envelope.bodyOut = request;
			HttpTransportSE ht = new HttpTransportSE(wsUrl);
			ht.call(null, envelope);
			Object soapObject = (Object) envelope.getResponse();
			//		System.out.println(soapObject.toString());
			result=soapObject.toString();
		}
		catch(Exception ex){

		}
		return result;
	}
	/**
	 * @param methodName
	 * @return 传入参数及方法名获取服务端返回值
	 * @throws Exception
	 * @throws XmlPullParserException
	 */
	public static String soapGetInfo(String methodName , String[] key , String[] value) throws Exception, XmlPullParserException {

		// 返回的查询结果
		String result = null;
		// 将方法名和名称空间绑定在一起
		String SOAP_ACTION = nameSpace + methodName;

		SoapObject request = new SoapObject(nameSpace, methodName);
		//传参
		for(int i = 0;i<key.length;i++){
			request.addProperty(key[i], value[i]);
		}
		//加密信息
		Element[] header = new Element[1];
		header[0] = new Element().createElement("", "AuthenticationToken");
		Element username = new Element().createElement("", "username");
		username.addChild(Node.TEXT, WS_UNAME);
		header[0].addChild(Node.ELEMENT, username);
		Element pass = new Element().createElement("", "password");
		pass.addChild(Node.TEXT, WS_PWD);
		header[0].addChild(Node.ELEMENT, pass);
		// 设置soap的版本
		SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
		// 设置是否调用的是dotNet开发的
		envelope.dotNet = true;
		//添加通讯内容的 header、body
		envelope.headerOut = header;
		envelope.bodyOut = request;
		envelope.setOutputSoapObject(request);
		HttpTransportSE hts;
		//获取代理主机
//		String host = android.net.Proxy.getDefaultHost();
		// 获取端口
//		int port = android.net.Proxy.getDefaultPort();
		hts=new HttpTransportSE(wsBaseUrl);
		try
		{
			// web service请求
			hts.call(SOAP_ACTION, envelope);
			// 得到返回结果
			//Object o = envelope.getResponse();
			Object o = (Object)envelope.getResponse();
			result = o.toString();
		}
		catch(Exception ex)
		{
			String err=ex.getMessage();

		}

		return result;
	}

}
